package game.round.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import game.level.pojo.ALevel;
import game.level.service.IALevelService;
import game.level.service.impl.ALevelServiceImpl;
import game.round.dao.IARoundDao;
import game.round.dao.impl.ARoundDaoImpl;
import game.round.pojo.ARound;
import game.round.service.ARoundService;
import game.users.pojo.AUsers;
import game.users.service.IAUsersService;
import game.users.service.impl.AUsersServiceImpl;

public class ARoundServiceImpl implements ARoundService
{
	private IARoundDao iARounDao = new ARoundDaoImpl();
	
	private IAUsersService usersService = new AUsersServiceImpl();
	private IALevelService levelService = new ALevelServiceImpl();
	
	public int insertRondService(ARound round)
	{
		/* 要为此用户判断一下是否要升级
		 * 
		 *  ~给用户增加积分*/
		Map<String, Object> condMap = new HashMap<String, Object>();
		
		condMap.put("id", round.getUsersId());
		
		AUsers users = this.usersService.findOndAUsersService(condMap);
		condMap.clear();
		/* 赢得次数 * 每一局的得分 */
		users.setScore(users.getScore()+round.getWinCount()*round.getPerScore());
		/*更新时间*/
		users.setUpdateTime(new Date());
		condMap.put("currScore",users.getScore());
		/* 开始的分数<用户当前的分数<结束的分数 */
//		ALevel level = this.levelService.findOneLevelService(condMap);
//		users.setLevelId(level.getId());
		this.usersService.updateService(users);
		return this.iARounDao.insertDao(round);
	}

	@Override
	public int updateRondService(ARound round)
	{
		return this.iARounDao.updateDao(round);
	}

	@Override
	public int deleteRondService(ARound round)
	{
		return this.iARounDao.deleteDao(round);
	}

	@Override
	public ARound findOneRondService(Map<String, Object> conMap)
	{
		return this.iARounDao.findOneDao(conMap);
	}
	@Override
	public List<ARound> findCondListRondService(Map<String, Object> conMap)
	{
		
		List<ARound> roundList = this.iARounDao.findCondListDao(conMap);
		/*此Map只为了查询关联关系*/
		Map<String, Object> extMap = new HashMap<String, Object>();
		/*查询局表的关联的用户表*/
		for (Iterator iterator = roundList.iterator(); iterator.hasNext();)
		{
			ARound round = (ARound) iterator.next();
			extMap.clear();
			extMap.put("id",round.getUsersId());
			AUsers users = this.usersService.findOndAUsersService(extMap);
			/*将查询出来的结果给Round*/
			round.setUsers(users);
		}
		return roundList;
	}
}
